home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / exec.test < prev    next >
Text File  |  1993-07-01  |  15KB  |  429 lines

  1. # Commands covered:  exec
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/exec.test,v 1.27 93/07/01 15:22:35 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. # Basic operations.
  32.  
  33. test exec-1.1 {basic exec operation} {
  34.     exec echo a b c
  35. } "a b c"
  36. test exec-1.2 {pipelining} {
  37.     exec echo a b c d | cat | cat
  38. } "a b c d"
  39. test exec-1.3 {pipelining} {
  40.     set a [exec echo a b c d | cat | wc]
  41.     list [scan $a "%d %d %d" b c d] $b $c $d
  42. } {3 1 4 8}
  43.  
  44. # I/O redirection: input from Tcl command.
  45.  
  46. test exec-2.1 {redirecting input from immediate source} {
  47.     exec cat << "Sample text"
  48. } {Sample text}
  49. test exec-2.2 {redirecting input from immediate source} {
  50.     exec << "Sample text" cat | cat
  51. } {Sample text}
  52. test exec-2.3 {redirecting input from immediate source} {
  53.     exec cat << "Sample text" | cat
  54. } {Sample text}
  55. test exec-2.4 {redirecting input from immediate source} {
  56.     exec  cat | cat << "Sample text"
  57. } {Sample text}
  58. test exec-2.5 {redirecting input from immediate source} {
  59.     exec cat "<<Joined to arrows"
  60. } {Joined to arrows}
  61.  
  62. # I/O redirection: output to file.
  63.  
  64. catch {exec rm -f gorp.file}
  65. test exec-3.1 {redirecting output to file} {
  66.     exec echo "Some simple words" > gorp.file
  67.     exec cat gorp.file
  68. } "Some simple words"
  69. test exec-3.2 {redirecting output to file} {
  70.     exec echo "More simple words" | >gorp.file cat | cat
  71.     exec cat gorp.file
  72. } "More simple words"
  73. test exec-3.3 {redirecting output to file} {
  74.     exec > gorp.file echo "Different simple words" | cat | cat
  75.     exec cat gorp.file
  76. } "Different simple words"
  77. test exec-3.4 {redirecting output to file} {
  78.     exec echo "Some simple words" >gorp.file
  79.     exec cat gorp.file
  80. } "Some simple words"
  81. test exec-3.5 {redirecting output to file} {
  82.     exec echo "First line" >gorp.file
  83.     exec echo "Second line" >> gorp.file
  84.     exec cat gorp.file
  85. } "First line\nSecond line"
  86. test exec-3.6 {redirecting output to file} {
  87.     exec echo "First line" >gorp.file
  88.     exec echo "Second line" >>gorp.file
  89.     exec cat gorp.file
  90. } "First line\nSecond line"
  91. test exec-3.7 {redirecting output to file} {
  92.     set f [open gorp.file w]
  93.     puts $f "Line 1"
  94.     flush $f
  95.     exec echo "More text" >@ $f
  96.     exec echo >@$f "Even more"
  97.     puts $f "Line 3"
  98.     close $f
  99.     exec cat gorp.file
  100. } "Line 1\nMore text\nEven more\nLine 3"
  101.  
  102. # I/O redirection: output and stderr to file.
  103.  
  104. catch {exec rm -f gorp.file}
  105. test exec-4.1 {redirecting output and stderr to file} {
  106.     exec echo "test output" >& gorp.file
  107.     exec cat gorp.file
  108. } "test output"
  109. test exec-4.2 {redirecting output and stderr to file} {
  110.     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
  111.         [exec cat gorp.file]
  112. } {{} {foo bar}}
  113. test exec-4.3 {redirecting output and stderr to file} {
  114.     exec echo "first line" > gorp.file
  115.     list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
  116.         [exec cat gorp.file]
  117. } "{} {first line\nfoo bar}"
  118. test exec-4.4 {redirecting output and stderr to file} {
  119.     set f [open gorp.file w]
  120.     puts $f "Line 1"
  121.     flush $f
  122.     exec echo "More text" >&@ $f
  123.     exec echo >&@$f "Even more"
  124.     puts $f "Line 3"
  125.     close $f
  126.     exec cat gorp.file
  127. } "Line 1\nMore text\nEven more\nLine 3"
  128. test exec-4.5 {redirecting output and stderr to file} {
  129.     set f [open gorp.file w]
  130.     puts $f "Line 1"
  131.     flush $f
  132.     exec >&@ $f sh -c "echo foo bar 1>&2"
  133.     exec >&@$f sh -c "echo xyzzy 1>&2"
  134.     puts $f "Line 3"
  135.     close $f
  136.     exec cat gorp.file
  137. } "Line 1\nfoo bar\nxyzzy\nLine 3"
  138.  
  139. # I/O redirection: input from file.
  140.  
  141. exec echo "Just a few thoughts" > gorp.file
  142. test exec-5.1 {redirecting input from file} {
  143.     exec cat < gorp.file
  144. } {Just a few thoughts}
  145. test exec-5.2 {redirecting input from file} {
  146.     exec cat | cat < gorp.file
  147. } {Just a few thoughts}
  148. test exec-5.3 {redirecting input from file} {
  149.     exec cat < gorp.file | cat
  150. } {Just a few thoughts}
  151. test exec-5.4 {redirecting input from file} {
  152.     exec < gorp.file cat | cat
  153. } {Just a few thoughts}
  154. test exec-5.5 {redirecting input from file} {
  155.     exec cat <gorp.file
  156. } {Just a few thoughts}
  157. test exec-5.6 {redirecting input from file} {
  158.     set f [open gorp.file r]
  159.     set result [exec cat <@ $f]
  160.     close $f
  161.     set result
  162. } {Just a few thoughts}
  163. test exec-5.7 {redirecting input from file} {
  164.     set f [open gorp.file r]
  165.     set result [exec <@$f cat]
  166.     close $f
  167.     set result
  168. } {Just a few thoughts}
  169.  
  170. # I/O redirection: standard error through a pipeline.
  171.  
  172. test exec-6.1 {redirecting stderr through a pipeline} {
  173.     exec sh -c "echo foo bar" |& cat
  174. } "foo bar"
  175. test exec-6.2 {redirecting stderr through a pipeline} {
  176.     exec sh -c "echo foo bar 1>&2" |& cat
  177. } "foo bar"
  178. test exec-6.3 {redirecting stderr through a pipeline} {
  179.     exec sh -c "echo foo bar 1>&2" |& sh -c "echo second msg 1>& 2; cat" |& cat
  180. } "second msg\nfoo bar"
  181.  
  182. # I/O redirection: combinations.
  183.  
  184. catch {exec rm -f gorp.file2}
  185. test exec-7.1 {multiple I/O redirections} {
  186.     exec << "command input" > gorp.file2 cat < gorp.file
  187.     exec cat gorp.file2
  188. } {Just a few thoughts}
  189. test exec-7.2 {multiple I/O redirections} {
  190.     exec < gorp.file << "command input" cat
  191. } {command input}
  192.  
  193. # Long input to command and output from command.
  194.  
  195. set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
  196. set a [concat $a $a $a $a]
  197. set a [concat $a $a $a $a]
  198. set a [concat $a $a $a $a]
  199. set a [concat $a $a $a $a]
  200. test exec-8.1 {long input and output} {
  201.     exec cat << $a
  202. } $a
  203.  
  204. # Commands that return errors.
  205.  
  206. test exec-9.1 {commands returning errors} {
  207.     set x [catch {exec gorp456} msg]
  208.     list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end]
  209. } {1 {couldn't find "gorp456" to execute} CHILDSTATUS 1}
  210. test exec-9.2 {commands returning errors} {
  211.     set x [catch {exec foo123 | gorp456} msg]
  212.     set x1 {couldn't find "foo123" to execute
  213. couldn't find "gorp456" to execute}
  214.     set x2 {couldn't find "gorp456" to execute
  215. couldn't find "foo123" to execute}
  216.     set y [expr {($msg == $x1) || ($msg == $x2)}]
  217.     list $x $y [lindex $errorCode 0] [lrange $errorCode 2 end]
  218. } {1 1 CHILDSTATUS 1}
  219. test exec-9.3 {commands returning errors} {
  220.     list [catch {exec sleep 1 | sh -c "exit 43" | sleep 1} msg] $msg
  221. } {1 {child process exited abnormally}}
  222. test exec-9.4 {commands returning errors} {
  223.     list [catch {exec gorp456 | echo a b c} msg] $msg
  224. } {1 {a b c
  225. couldn't find "gorp456" to execute}}
  226. test exec-9.5 {commands returning errors} {
  227.     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
  228. } {1 {error msg}}
  229. test exec-9.6 {commands returning errors} {
  230.     list [catch {exec sh -c "echo error msg 1>&2" | sh -c "echo error msg 1>&2"} msg] $msg
  231. } {1 {error msg
  232. error msg}}
  233.  
  234. # Errors in executing the Tcl command, as opposed to errors in the
  235. # processes that are invoked.
  236.  
  237. test exec-10.1 {errors in exec invocation} {
  238.     list [catch {exec} msg] $msg
  239. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  240. test exec-10.2 {errors in exec invocation} {
  241.     list [catch {exec | cat} msg] $msg
  242. } {1 {illegal use of | or |& in command}}
  243. test exec-10.3 {errors in exec invocation} {
  244.     list [catch {exec cat |} msg] $msg
  245. } {1 {illegal use of | or |& in command}}
  246. test exec-10.4 {errors in exec invocation} {
  247.     list [catch {exec cat | | cat} msg] $msg
  248. } {1 {illegal use of | or |& in command}}
  249. test exec-10.5 {errors in exec invocation} {
  250.     list [catch {exec cat | |& cat} msg] $msg
  251. } {1 {illegal use of | or |& in command}}
  252. test exec-10.6 {errors in exec invocation} {
  253.     list [catch {exec cat |&} msg] $msg
  254. } {1 {illegal use of | or |& in command}}
  255. test exec-10.7 {errors in exec invocation} {
  256.     list [catch {exec cat <} msg] $msg
  257. } {1 {can't specify "<" as last word in command}}
  258. test exec-10.8 {errors in exec invocation} {
  259.     list [catch {exec cat >} msg] $msg
  260. } {1 {can't specify ">" as last word in command}}
  261. test exec-10.9 {errors in exec invocation} {
  262.     list [catch {exec cat <<} msg] $msg
  263. } {1 {can't specify "<<" as last word in command}}
  264. test exec-10.10 {errors in exec invocation} {
  265.     list [catch {exec cat >>} msg] $msg
  266. } {1 {can't specify ">>" as last word in command}}
  267. test exec-10.11 {errors in exec invocation} {
  268.     list [catch {exec cat >&} msg] $msg
  269. } {1 {can't specify ">&" as last word in command}}
  270. test exec-10.12 {errors in exec invocation} {
  271.     list [catch {exec cat >>&} msg] $msg
  272. } {1 {can't specify ">>&" as last word in command}}
  273. test exec-10.13 {errors in exec invocation} {
  274.     list [catch {exec cat >@} msg] $msg
  275. } {1 {can't specify ">@" as last word in command}}
  276. test exec-10.14 {errors in exec invocation} {
  277.     list [catch {exec cat <@} msg] $msg
  278. } {1 {can't specify "<@" as last word in command}}
  279. test exec-10.15 {errors in exec invocation} {
  280.     list [catch {exec cat < a/b/c} msg] [string tolower $msg]
  281. } {1 {couldn't read file "a/b/c": no such file or directory}}
  282. test exec-10.16 {errors in exec invocation} {
  283.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  284. } {1 {couldn't write file "a/b/c": no such file or directory}}
  285. test exec-10.17 {errors in exec invocation} {
  286.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  287. } {1 {couldn't write file "a/b/c": no such file or directory}}
  288. set f [open gorp.file w]
  289. test exec-10.18 {errors in exec invocation} {
  290.     list [catch {exec cat <@ $f} msg] $msg
  291. } "1 {\"$f\" wasn't opened for reading}"
  292. close $f
  293. set f [open gorp.file r]
  294. test exec-10.19 {errors in exec invocation} {
  295.     list [catch {exec cat >@ $f} msg] $msg
  296. } "1 {\"$f\" wasn't opened for writing}"
  297. close $f
  298.  
  299. # Commands in background.
  300.  
  301. test exec-11.1 {commands in background} {
  302.     set x [lindex [time {exec sleep 2 &}] 0]
  303.     expr $x<1000000
  304. } 1
  305. test exec-11.2 {commands in background} {
  306.     list [catch {exec echo a &b} msg] $msg
  307. } {0 {a &b}}
  308. test exec-11.3 {commands in background} {
  309.     llength [exec sleep 1 &]
  310. } 1
  311. test exec-11.4 {commands in background} {
  312.     llength [exec sleep 1 | sleep 1 | sleep 1 &]
  313. } 3
  314.  
  315. # Make sure that background commands are properly reaped when
  316. # they eventually die.
  317.  
  318. exec sleep 3
  319. if $atBerkeley {
  320.     test exec-12.1 {reaping background processes} {
  321.     for {set i 0} {$i < 20} {incr i} {
  322.         exec echo foo > /dev/null &
  323.     }
  324.     exec sleep 1
  325.     catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
  326.     lindex $msg 0
  327.     } 0
  328.     test exec-12.2 {reaping background processes} {
  329.     exec sleep 2 | sleep 2 | sleep 2 &
  330.     catch {exec ps | fgrep "sleep 2" | fgrep -v fgrep | wc} msg
  331.     set x [lindex $msg 0]
  332.     exec sleep 3
  333.     catch {exec ps | fgrep "sleep 2" | fgrep -v fgrep | wc} msg
  334.     list $x [lindex $msg 0]
  335.     } {3 0}
  336.     test exec-12.3 {reaping background processes} {
  337.     exec sleep 1000 &
  338.     exec sleep 1000 &
  339.     set x [exec ps | fgrep "sleep 1000" | fgrep -v fgrep]
  340.     set pids {}
  341.     foreach i [split $x \n] {
  342.         lappend pids [lindex $i 0]
  343.     }
  344.     foreach i $pids {
  345.         catch {exec kill -STOP $i}
  346.     }
  347.     catch {exec ps | fgrep "sleep 1000" | fgrep -v fgrep | wc} msg
  348.     set x [lindex $msg 0]
  349.     
  350.     foreach i $pids {
  351.         catch {exec kill -KILL $i}
  352.     }
  353.     catch {exec ps | fgrep "sleep 1000" | fgrep -v fgrep | wc} msg
  354.     list $x [lindex $msg 0]
  355.     } {2 0}
  356. }
  357.  
  358. # Make sure "errorCode" is set correctly.
  359.  
  360. test exec-13.1 {setting errorCode variable} {
  361.     list [catch {exec cat < a/b/c} msg] [string tolower $errorCode]
  362. } {1 {posix enoent {no such file or directory}}}
  363. test exec-13.2 {setting errorCode variable} {
  364.     list [catch {exec cat > a/b/c} msg] [string tolower $errorCode]
  365. } {1 {posix enoent {no such file or directory}}}
  366. test exec-13.3 {setting errorCode variable} {
  367.     set x [catch {exec _weirdo_command_} msg]
  368.     list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end]
  369. } {1 {couldn't find "_weirdo_command_" to execute} CHILDSTATUS 1}
  370.  
  371. # Switches before the first argument
  372.  
  373. test exec-14.1 {-keepnewline switch} {
  374.     exec -keepnewline echo foo
  375. } "foo\n"
  376. test exec-14.2 {-keepnewline switch} {
  377.     list [catch {exec -keepnewline} msg] $msg
  378. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  379. test exec-14.3 {unknown switch} {
  380.     list [catch {exec -gorp} msg] $msg
  381. } {1 {bad switch "-gorp": must be -keepnewline or --}}
  382. test exec-14.4 {-- switch} {
  383.     list [catch {exec -- -gorp} msg] $msg
  384. } {1 {couldn't find "-gorp" to execute}}
  385.  
  386. # Redirecting standard error separately from standard output
  387.  
  388. test exec-15.1 {standard error redirection} {
  389.     exec echo "First line" > gorp.file
  390.     list [exec sh -c "echo foo bar 1>&2" >2 gorp.file] \
  391.         [exec cat gorp.file]
  392. } {{} {foo bar}}
  393. test exec-15.2 {standard error redirection} {
  394.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz >gorp.file \
  395.         >2 gorp.file2] [exec cat gorp.file] \
  396.         [exec cat gorp.file2]
  397. } {{} {biz baz} {foo bar}}
  398. test exec-15.3 {standard error redirection} {
  399.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz >2gorp.file \
  400.         > gorp.file2] [exec cat gorp.file] \
  401.         [exec cat gorp.file2]
  402. } {{} {foo bar} {biz baz}}
  403. test exec-15.4 {standard error redirection} {
  404.     set f [open gorp.file w]
  405.     puts $f "Line 1"
  406.     flush $f
  407.     exec sh -c "echo foo bar 1>&2" >2@ $f
  408.     puts $f "Line 3"
  409.     close $f
  410.     exec cat gorp.file
  411. } {Line 1
  412. foo bar
  413. Line 3}
  414. test exec-15.5 {standard error redirection} {
  415.     exec echo "First line" > gorp.file
  416.     exec sh -c "echo foo bar 1>&2" >>2 gorp.file
  417.     exec cat gorp.file
  418. } {First line
  419. foo bar}
  420. test exec-15.6 {standard error redirection} {
  421.     exec sh -c "echo foo bar 1>&2" > gorp.file2 >2 gorp.file \
  422.         >& gorp.file >2 gorp.file2 | echo biz baz
  423.     list [exec cat gorp.file] [exec cat gorp.file2]
  424. } {{biz baz} {foo bar}}
  425.  
  426. catch {exec rm -f gorp.file}
  427. catch {exec rm -f gorp.file2}
  428. return {}
  429.